<!DOCTYPE html>
<html lang="zh-CN" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>影片管理 - 经理仪表盘</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: 'Microsoft YaHei', Arial, sans-serif;
            background-color: #f5f5f5;
            line-height: 1.6;
        }
        
        .header {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 1rem 0;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        
        .nav {
            max-width: 1200px;
            margin: 0 auto;
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 0 20px;
        }
        
        .logo {
            font-size: 24px;
            font-weight: bold;
        }
        
        .nav-links {
            display: flex;
            gap: 30px;
        }
        
        .nav-links a {
            color: white;
            text-decoration: none;
            font-weight: 500;
            transition: opacity 0.3s;
        }
        
        .nav-links a:hover {
            opacity: 0.8;
        }
        
        .user-info {
            display: flex;
            align-items: center;
            gap: 15px;
        }
        
        .container {
            max-width: 1200px;
            margin: 0 auto;
            padding: 20px;
        }
        
        .section-title {
            font-size: 24px;
            color: #333;
            margin-bottom: 20px;
            border-bottom: 3px solid #667eea;
            padding-bottom: 10px;
        }
        
        .card {
            background: white;
            border-radius: 15px;
            box-shadow: 0 5px 15px rgba(0,0,0,0.1);
            margin-bottom: 30px;
            overflow: hidden;
        }
        
        .card-header {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 20px;
            font-size: 18px;
            font-weight: bold;
        }
        
        .card-body {
            padding: 30px;
        }
        
        .btn {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border: none;
            padding: 12px 24px;
            border-radius: 8px;
            cursor: pointer;
            text-decoration: none;
            display: inline-block;
            font-weight: 500;
            transition: transform 0.2s, box-shadow 0.2s;
        }
        
        .btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(0,0,0,0.2);
            color: white;
            text-decoration: none;
        }
        
        .btn-sm {
            padding: 8px 16px;
            font-size: 14px;
        }
        
        .btn-success {
            background: linear-gradient(135deg, #28a745 0%, #20c997 100%);
        }
        
        .btn-danger {
            background: linear-gradient(135deg, #dc3545 0%, #fd7e14 100%);
        }
        
        .btn-warning {
            background: linear-gradient(135deg, #ffc107 0%, #fd7e14 100%);
            color: #000;
        }
        
        .btn-secondary {
            background: linear-gradient(135deg, #6c757d 0%, #495057 100%);
        }
        
        .form-control {
            width: 100%;
            padding: 12px;
            border: 1px solid #ddd;
            border-radius: 8px;
            font-size: 14px;
            transition: border-color 0.3s;
        }
        
        .form-control:focus {
            outline: none;
            border-color: #667eea;
            box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
        }
        
        .table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 20px;
        }
        
        .table th,
        .table td {
            padding: 12px;
            text-align: left;
            border-bottom: 1px solid #e1e1e1;
        }
        
        .table th {
            background-color: #f8f9fa;
            font-weight: bold;
            color: #333;
        }
        
        .table tr:hover {
            background-color: #f8f9fa;
        }
        
        .alert {
            padding: 15px;
            border-radius: 8px;
            margin-bottom: 20px;
        }
        
        .alert-success {
            background-color: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }
        
        .alert-danger {
            background-color: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }
        
        .badge {
            padding: 4px 8px;
            border-radius: 4px;
            font-size: 12px;
            font-weight: bold;
        }
        
        .bg-success {
            background-color: #28a745;
            color: white;
        }
        
        .bg-warning {
            background-color: #ffc107;
            color: #000;
        }
        
        .bg-danger {
            background-color: #dc3545;
            color: white;
        }
        
        .bg-secondary {
            background-color: #6c757d;
            color: white;
        }
        
        .bg-primary {
            background-color: #667eea;
            color: white;
        }
        
        .text-center {
            text-align: center;
        }
        
        .text-muted {
            color: #666;
        }
        
        .grid-2 {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 30px;
        }
        
        .grid-3 {
            display: grid;
            grid-template-columns: repeat(3, 1fr);
            gap: 20px;
        }
        
        .modal {
            display: none;
            position: fixed;
            z-index: 1000;
            left: 0;
            top: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0,0,0,0.5);
        }
        
        .modal-dialog {
            position: relative;
            margin: 50px auto;
            max-width: 600px;
            background: white;
            border-radius: 15px;
            box-shadow: 0 10px 30px rgba(0,0,0,0.3);
        }
        
        .modal-header {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 20px;
            border-radius: 15px 15px 0 0;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        
        .modal-body {
            padding: 30px;
        }
        
        .modal-footer {
            padding: 20px;
            border-top: 1px solid #e1e1e1;
            display: flex;
            justify-content: flex-end;
            gap: 10px;
        }
        
        .close {
            background: none;
            border: none;
            color: white;
            font-size: 24px;
            cursor: pointer;
        }
        
        .form-group {
            margin-bottom: 20px;
        }
        
        .form-label {
            display: block;
            margin-bottom: 8px;
            font-weight: 500;
            color: #333;
        }
        
        .form-select {
            width: 100%;
            padding: 12px;
            border: 1px solid #ddd;
            border-radius: 8px;
            font-size: 14px;
            background-color: white;
        }
        
        /* 添加影片卡片样式 */
        .add-movie-card {
            background: linear-gradient(135deg, #f8f9fa 0%, #e9ecef 100%);
            border: 2px dashed #dee2e6;
            transition: all 0.3s ease;
            position: relative;
            overflow: hidden;
        }
        
        .add-movie-card:hover {
            border-color: #667eea;
            background: linear-gradient(135deg, #fff 0%, #f8f9fa 100%);
            transform: translateY(-2px);
            box-shadow: 0 8px 25px rgba(102, 126, 234, 0.15);
        }
        
        .add-movie-card::before {
            content: '';
            position: absolute;
            top: -50%;
            left: -50%;
            width: 200%;
            height: 200%;
            background: linear-gradient(45deg, transparent, rgba(102, 126, 234, 0.1), transparent);
            transform: rotate(45deg);
            transition: all 0.6s ease;
            opacity: 0;
        }
        
        .add-movie-card:hover::before {
            animation: shimmer 1.5s ease-in-out;
        }
        
        @keyframes shimmer {
            0% {
                transform: translateX(-100%) translateY(-100%) rotate(45deg);
                opacity: 0;
            }
            50% {
                opacity: 1;
            }
            100% {
                transform: translateX(100%) translateY(100%) rotate(45deg);
                opacity: 0;
            }
        }
        
        .add-movie-icon {
            font-size: 48px;
            margin-bottom: 15px;
            animation: bounce 2s infinite;
            filter: drop-shadow(0 4px 8px rgba(0,0,0,0.1));
        }
        
        @keyframes bounce {
            0%, 20%, 50%, 80%, 100% {
                transform: translateY(0);
            }
            40% {
                transform: translateY(-10px);
            }
            60% {
                transform: translateY(-5px);
            }
        }
        
        .add-movie-title {
            color: #495057;
            font-size: 20px;
            font-weight: 600;
            margin-bottom: 10px;
            letter-spacing: 0.5px;
        }
        
        .add-movie-description {
            color: #6c757d;
            font-size: 14px;
            margin-bottom: 25px;
            line-height: 1.5;
        }
        
        .add-movie-btn {
            background: linear-gradient(135deg, #28a745 0%, #20c997 100%);
            border: none;
            padding: 15px 30px;
            font-size: 16px;
            font-weight: 600;
            border-radius: 50px;
            box-shadow: 0 4px 15px rgba(40, 167, 69, 0.3);
            transition: all 0.3s ease;
            position: relative;
            overflow: hidden;
        }
        
        .add-movie-btn:hover {
            background: linear-gradient(135deg, #218838 0%, #1e7e34 100%);
            transform: translateY(-2px);
            box-shadow: 0 6px 20px rgba(40, 167, 69, 0.4);
        }
        
        .add-movie-btn:active {
            transform: translateY(0);
            box-shadow: 0 2px 10px rgba(40, 167, 69, 0.3);
        }
        
        .btn-icon {
            display: inline-block;
            margin-right: 8px;
            transition: transform 0.3s ease;
        }
        
        .add-movie-btn:hover .btn-icon {
            transform: rotate(90deg) scale(1.1);
        }
        
        .btn-text {
            position: relative;
            z-index: 1;
        }
        
        @media (max-width: 768px) {
            .grid-2, .grid-3 {
                grid-template-columns: 1fr;
            }
            
            .nav {
                flex-direction: column;
                gap: 15px;
            }
            
            .nav-links {
                gap: 15px;
            }
        }
    </style>
</head>
<body>
    <header class="header">
        <nav class="nav">
            <div class="logo">🎬 经理控制台</div>
            <div class="nav-links">
                <a href="/manager/dashboard">控制台</a>
                <a href="/manager/password-management">密码管理</a>
                <a href="/manager/movies">影片管理</a>
                <a href="/manager/screenings">排片管理</a>
                <a href="/manager/sales-data">销售数据</a>
            </div>
            <div class="user-info">
                <span th:text="'经理：' + ${session.user != null ? session.user.username : '未登录'}">经理</span>
                <a href="/auth/logout" class="btn btn-sm">退出登录</a>
            </div>
        </nav>
    </header>

    <div class="container">
        <h2 class="section-title">🎬 影片管理</h2>

        <!-- 成功/错误消息 -->
        <div th:if="${message}" class="alert alert-success">
            ✅ <span th:text="${message}"></span>
        </div>
        <div th:if="${error}" class="alert alert-danger">
            ❌ <span th:text="${error}"></span>
        </div>

        <!-- 搜索和添加 -->
        <div class="grid-2">
            <div class="card">
                <div class="card-header">
                    🔍 搜索影片
                </div>
                <div class="card-body">
                    <form action="/manager/movies/search" method="get">
                        <div class="grid-3">
                            <input type="text" class="form-control" name="title" placeholder="影片名称" th:value="${searchTitle}">
                            <input type="text" class="form-control" name="director" placeholder="导演" th:value="${searchDirector}">
                            <input type="text" class="form-control" name="actors" placeholder="主演" th:value="${searchActors}">
                        </div>
                        <div style="margin-top: 20px;">
                            <button type="submit" class="btn">
                                🔍 搜索
                            </button>
                            <a href="/manager/movies" class="btn btn-secondary" style="margin-left: 10px;">
                                🔄 重置
                            </a>
                        </div>
                    </form>
                </div>
            </div>
            <div class="card add-movie-card">
                <div class="card-body text-center">
                    <div class="add-movie-icon">
                        🎬
                    </div>
                    <h4 class="add-movie-title">添加新影片</h4>
                    <p class="add-movie-description">点击下方按钮添加新的影片信息</p>
                    <button type="button" class="btn btn-success add-movie-btn" onclick="openAddMovieModal()">
                        <span class="btn-icon">➕</span>
                        <span class="btn-text">添加影片</span>
                    </button>
                </div>
            </div>
        </div>

        <!-- 影片列表 -->
        <div class="card">
            <div class="card-header">
                📋 影片列表 <span class="badge bg-primary" th:text="${#lists.size(movies)}">0</span>
            </div>
            <div class="card-body">
                <div style="overflow-x: auto;">
                    <table class="table">
                        <thead>
                            <tr>
                                <th>片名</th>
                                <th>导演</th>
                                <th>主演</th>
                                <th style="width: 100px;">时长</th>
                                <th style="width: 100px;">评分</th>
                                <th style="width: 120px;">状态</th>
                                <th>操作</th>
                            </tr>
                        </thead>
                        <tbody>
                            <tr th:each="movie : ${movies}">
                                <td>
                                    <strong th:text="${movie.title}"></strong>
                                    <br>
                                    <small class="text-muted" th:text="${#strings.abbreviate(movie.synopsis, 50)}"></small>
                                </td>
                                <td th:text="${movie.director}"></td>
                                <td th:text="${movie.actors}"></td>
                                <td th:text="${movie.duration} + '分钟'"></td>
                                <td>
                                    <span class="badge" th:classappend="${movie.rating >= 8 ? 'bg-success' : (movie.rating >= 6 ? 'bg-warning' : 'bg-danger')}">
                                        ⭐ <span th:text="${movie.rating}"></span>
                                    </span>
                                </td>
                                <td>
                                    <span th:if="${movie.status == '正在上映'}" class="badge bg-success">
                                        ▶️ 上映中
                                    </span>
                                    <span th:unless="${movie.status == '正在上映'}" class="badge bg-secondary">
                                        ⏸️ 未上映
                                    </span>
                                </td>
                                <td>
                                    <button type="button" class="btn btn-sm" 
                                            onclick="editMovie(this)" 
                                            th:data-movie-id="${movie.id}"
                                            th:data-title="${movie.title}"
                                            th:data-director="${movie.director}"
                                            th:data-actors="${movie.actors}"
                                            th:data-synopsis="${movie.synopsis}"
                                            th:data-duration="${movie.duration}"
                                            th:data-rating="${movie.rating}"
                                            th:data-showing="${movie.status == '正在上映'}" style="margin-right: 5px;">
                                        ✏️ 编辑
                                    </button>
                                    <form th:action="@{/manager/movies/delete/{movieId}(movieId=${movie.id})}" method="post" style="display: inline;" 
                                          onsubmit="return confirmDelete(this)">
                                        <input type="hidden" name="movieTitle" th:value="${movie.title}">
                                        <button type="submit" class="btn btn-sm btn-danger">
                                            🗑️ 删除
                                        </button>
                                    </form>
                                </td>
                            </tr>
                            <tr th:if="${#lists.isEmpty(movies)}">
                                <td colspan="7" class="text-center text-muted">
                                    <div style="padding: 40px 0;">
                                        <div style="font-size: 48px; margin-bottom: 20px;">🎬</div>
                                        <p>暂无影片数据</p>
                                    </div>
                                </td>
                            </tr>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>

    <!-- 添加影片模态框 -->
    <div class="modal" id="addMovieModal">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h5>➕ 添加影片</h5>
                    <button type="button" class="close" onclick="closeAddMovieModal()">&times;</button>
                </div>
                <form action="/manager/movies/add" method="post">
                    <div class="modal-body">
                        <div class="grid-2">
                            <div>
                                <div class="form-group">
                                    <label class="form-label">片名 *</label>
                                    <input type="text" class="form-control" name="title" required>
                                </div>
                                <div class="form-group">
                                    <label class="form-label">导演 *</label>
                                    <input type="text" class="form-control" name="director" required>
                                </div>
                                <div class="form-group">
                                    <label class="form-label">主演 *</label>
                                    <input type="text" class="form-control" name="actors" required>
                                </div>
                            </div>
                            <div>
                                <div class="form-group">
                                    <label class="form-label">时长(分钟) *</label>
                                    <input type="number" class="form-control" name="duration" min="1" required>
                                </div>
                                <div class="form-group">
                                    <label class="form-label">评分(0-10) *</label>
                                    <input type="number" class="form-control" name="rating" min="0" max="10" step="0.1" required>
                                </div>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="form-label">剧情简介 *</label>
                            <textarea class="form-control" name="synopsis" rows="4" required></textarea>
                        </div>
                        <div class="form-group">
                            <label class="form-label">
                                <input type="checkbox" name="status" value="正在上映" style="margin-right: 8px;">
                                正在上映
                            </label>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" onclick="closeAddMovieModal()">❌ 取消</button>
                        <button type="submit" class="btn btn-success">
                            💾 添加影片
                        </button>
                    </div>
                </form>
            </div>
        </div>
    </div>

    <!-- 编辑影片模态框 -->
    <div class="modal" id="editMovieModal">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h5>✏️ 编辑影片</h5>
                    <button type="button" class="close" onclick="closeEditMovieModal()">&times;</button>
                </div>
                <form action="/manager/movies/edit" method="post">
                    <input type="hidden" id="editMovieId" name="movieId">
                    <div class="modal-body">
                        <div class="grid-2">
                            <div>
                                <div class="form-group">
                                    <label class="form-label">片名 *</label>
                                    <input type="text" class="form-control" id="editTitle" name="title" required>
                                </div>
                                <div class="form-group">
                                    <label class="form-label">导演 *</label>
                                    <input type="text" class="form-control" id="editDirector" name="director" required>
                                </div>
                                <div class="form-group">
                                    <label class="form-label">主演 *</label>
                                    <input type="text" class="form-control" id="editActors" name="actors" required>
                                </div>
                            </div>
                            <div>
                                <div class="form-group">
                                    <label class="form-label">时长(分钟) *</label>
                                    <input type="number" class="form-control" id="editDuration" name="duration" min="1" required>
                                </div>
                                <div class="form-group">
                                    <label class="form-label">评分(0-10) *</label>
                                    <input type="number" class="form-control" id="editRating" name="rating" min="0" max="10" step="0.1" required>
                                </div>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="form-label">剧情简介 *</label>
                            <textarea class="form-control" id="editSynopsis" name="synopsis" rows="4" required></textarea>
                        </div>
                        <div class="form-group">
                            <label class="form-label">
                                <input type="checkbox" id="editIsShowing" name="status" value="正在上映" style="margin-right: 8px;">
                                正在上映
                            </label>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" onclick="closeEditMovieModal()">❌ 取消</button>
                        <button type="submit" class="btn">
                            💾 保存修改
                        </button>
                    </div>
                </form>
            </div>
        </div>
    </div>

    <script>
        function openAddMovieModal() {
            document.getElementById('addMovieModal').style.display = 'block';
        }
        
        function closeAddMovieModal() {
            document.getElementById('addMovieModal').style.display = 'none';
        }
        
        function closeEditMovieModal() {
            document.getElementById('editMovieModal').style.display = 'none';
        }
        
        function editMovie(button) {
            const movieId = button.getAttribute('data-movie-id');
            const title = button.getAttribute('data-title');
            const director = button.getAttribute('data-director');
            const actors = button.getAttribute('data-actors');
            const synopsis = button.getAttribute('data-synopsis');
            const duration = button.getAttribute('data-duration');
            const rating = button.getAttribute('data-rating');
            const isShowing = button.getAttribute('data-showing') === 'true';
            
            document.getElementById('editMovieId').value = movieId;
            document.getElementById('editTitle').value = title;
            document.getElementById('editDirector').value = director;
            document.getElementById('editActors').value = actors;
            document.getElementById('editSynopsis').value = synopsis;
            document.getElementById('editDuration').value = duration;
            document.getElementById('editRating').value = rating;
            document.getElementById('editIsShowing').checked = isShowing;
            
            document.getElementById('editMovieModal').style.display = 'block';
        }
        
        function confirmDelete(form) {
            const movieTitle = form.movieTitle.value;
            return confirm('确定要删除影片《' + movieTitle + '》吗？\n\n注意：删除后无法恢复！\n如果该影片正在上映且已有排片，则不允许删除。');
        }
        
        // 点击模态框外部关闭
        window.onclick = function(event) {
            const addModal = document.getElementById('addMovieModal');
            const editModal = document.getElementById('editMovieModal');
            if (event.target === addModal) {
                addModal.style.display = 'none';
            }
            if (event.target === editModal) {
                editModal.style.display = 'none';
            }
        }
    </script>
</body>
</html>